package com.example.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.myblog.entity.Follow;
import com.example.myblog.exception.BaseException;
import com.example.myblog.mapper.FollowMapper;
import com.example.myblog.result.BaseCodeEnum;
import com.example.myblog.service.FollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myblog.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 关注表 服务实现类
 * </p>
 *
 * @author 叶兴
 * @since 2022-09-29
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {
    FollowMapper followMapper;

    @Autowired
    public void setFollowMapper(FollowMapper followMapper) {
        this.followMapper = followMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int followUser(String followName) throws BaseException {
        // 1. 获取当前用户
        String username = UserThreadLocal.getUserDtoName();
        // 2. 判断是否有关注记录
        if (ifFollow(username,followName) == null) {
            // 2.1 无则新增
            Follow follow = new Follow();
            follow.setFollowName(followName);
            follow.setUserName(username);
            follow.setFollowTime(LocalDateTime.now());
            follow.setFollowState(1);
            int insert = followMapper.insert(follow);
            if (insert != 1) {
                throw new BaseException(BaseCodeEnum.INSERT_FAILED);
            }
        } else {
            if (ifFollow(username,followName).getFollowState() == 1) {
                throw new BaseException(BaseCodeEnum.HAS_FOLLOWED);
            }
            UpdateWrapper<Follow> wrapper = new UpdateWrapper<>();
            wrapper.eq("user_name",username).eq("follow_name",followName).set("follow_state",1);
            boolean update = update(wrapper);
            if (!update) {
                throw new BaseException(BaseCodeEnum.UPDATE_FAILED);
            }
        }
        return 0;
    }

    @Override
    public Follow ifFollow(String username, String followName)  {
        QueryWrapper<Follow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",username).eq("follow_name",followName);
        return followMapper.selectOne(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancel(String followName) throws BaseException {
        // 1. 获取当前用户
        String username = UserThreadLocal.getUserDtoName();
        // 2. 判断是否有关注记录
        if (ifFollow(username,followName) == null) {
            throw new BaseException(BaseCodeEnum.UNFOLLOWED);
        } else {
            if (ifFollow(username,followName).getFollowState() == 0) {
                throw new BaseException(BaseCodeEnum.UNFOLLOWED);
            }
            UpdateWrapper<Follow> wrapper = new UpdateWrapper<>();
            wrapper.eq("user_name",username).eq("follow_name",followName).set("follow_state",0);
            boolean update = update(wrapper);
            if (!update) {
                throw new BaseException(BaseCodeEnum.UPDATE_FAILED);
            }
        }
        return 0;
    }

    @Override
    public List<Follow> getFollows() throws BaseException {
        String username = UserThreadLocal.getUserDtoName();
        QueryWrapper<Follow> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",username).eq("follow_state",1);
        List<Follow> list = followMapper.selectList(wrapper);
        if (list == null || list.size() == 0) {
            throw new BaseException(BaseCodeEnum.NO_FOLLOWS);
        }
        return list;
    }
}
